<?php
/**
 *邀请记录
 */

namespace app\admin\model;


use app\common\model\Model;

class InviteRecord extends  Model
{
    const TABLE_NAME = 'invite_record';
    const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_1;

    const MAX_INVITE_CHAN_LEN = 3;
    protected $name = self::TABLE_NAME;


    //获取 邀请记录
    public static function getRecord($option=[])
    {
       $query = self::commonQuery(self::TABLE_NAME);

        if(is_numeric($option['user_id']))
        {
            $query->where(['user_id'=>$option['user_id']]);
        }else if(is_array($option['user_id']))
        {
            $query->where(['user_id'=>['in',$option['user_id']]]);
        }
        if(is_numeric($option['invite_user_id']))
        {
            $query->where(['invite_chan'=>['like',','.$option['invite_user_id'].'%']]);
        }else if(is_array($option['invite_user_id']))
        {
            if(count($option['invite_user_id']))
            {
                $query->where(['invite_chan'=>['like',','.$option['invite_user_id'][0].'%']]);

            }else{
                $like_value=[];
                foreach ($option['invite_user_id'] as $val)
                {
                    $like_value[]=['like',','.$val.'%'];
                }

                $like_value[]='or';
                $query->where(['invite_chan'=>$like_value[0]]);
            }
        }

        if(is_numeric($option['master_id']))
        {
            $query->where(['master_id'=>$option['master_id']]);
        }else if(is_array($option['master_id']))
        {
            $query->where(['master_id'=>['in',$option['master_id']]]);
        }
        return $query->order('user_id desc');


    }
    public static function handleInviteInfo($info)
    {

        $user_info =UserModel::getUserInfo($info['user_id']);
        $master_name= UserModel::getUserName($info['master_id']);
        $cur_invite_chan_arr = explode(',',substr($info['invite_chan'],1,-1));

        $invite_chan_info =[];
        foreach ($cur_invite_chan_arr as $key=>$val)
        {
            $name = UserModel::getUserName($val);
            $invite_chan_info[$key]=empty($name)?$val:$name;

        }

        return ['user_info'=>$user_info,'master_name'=>$master_name,
            'invite_chan_info'=>$invite_chan_info];
    }
}